<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\facade\Builder;
use think\App;
use think\facade\View;
use think\response\Json;

class Product extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Product';

    //关联模型
    protected array $hasOne = ['cate1', 'cate2'];

    public function index(): string
    {
        //构建table
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '商品名称', 'modify' => '2,input'])
            ->addColumn(['field' => 'images', 'title' => '缩略图', 'width' => 90, 'templet' => '#imagesTpl'])
            ->addColumn(['field' => 'cate_1', 'title' => '商品一级分类', 'modify' => '2,input', 'hide' => true])
            ->addColumn(['field' => 'cate_2', 'title' => '商品二级分类', 'modify' => '2,input', 'hide' => true])
            ->addColumn(['field' => 'cate', 'title' => '商品分类', 'templet' => '<div>{{d.cate1.title}}-{{d.cate2.title}}</div>'])
            ->addColumn(['field' => 'market_price', 'title' => '市场价', 'modify' => '2,input'])
            ->addColumn(['field' => 'price', 'title' => '会员价', 'modify' => '2,input'])
            ->addColumn(['field' => 'sales', 'title' => '销量', 'modify' => '2,input'])
            ->addColumn(['field' => 'stock', 'title' => '库存', 'modify' => '2,input'])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'templet' => '#sort_sort', 'width' => 120])
            ->addColumn(['field' => 'status', 'title' => '是否上架', 'templet' => '#status', 'width' => 120])
            ->addColumn(['field' => 'pick', 'title' => '推荐', 'templet' => '#pick', 'width' => 120])
            ->addColumn(['field' => 'content', 'title' => '详情', 'templet' => '#editorTpl'])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 150])
            ->addTopButtons(['add', 'deleteAll'])
            ->addRightButtons(['edit', 'delete'])
            ->addSortTool([['name' => 'sort', 'pk' => 'id', 'url' => url('update')->build()]])
            ->addSwitchTool([
                ['name' => 'status', 'pk' => 'id', 'text' => '上架|下架'],
                ['name' => 'pick', 'pk' => 'id', 'text' => '是|否']
            ])
            ->addSearch([
                ['title', '输入商品名称', 'input,Like']
            ])
            ->fetch();
    }

    public function add($id = '', $tpl = 'product/edit', $info = ''): string
    {
        View::assign('form', ['title' => '图片', 'field' => 'images', 'value' => 6]);

        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function edit($id = '', $tpl = 'product/edit', $info = ''): string
    {
        View::assign('form', ['title' => '图片', 'field' => 'images', 'value' => 6]);

        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function save($param = [], $model = false): Json
    {
        $param = $this->request->post();
        if (!empty($param['images'])) {
            $param['icon'] = explode(',', $param['images'])[0];
        }

        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }
}